/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.javanile.chesslang;

/**
 *
 * @author totalinnovation
 */
public class Less {
    static String square(int s) {
	return ""+(char)(97+s%8)+(char)(56-s/8);
    }
    static String square(byte s) {
	return ""+(char)(97+s%8)+(char)(56-s/8);
    }
    static String rank(int r) {
        return ""+(char)(97+r%8);
    }
    static String file(int f) {
        return ""+(char)(97+f%8);
    }

    static public String move(int f, int t) {
	return square(f)+square(t);
    }
    static public String move(int[] m) {
	return square(m[0])+square(m[1]);
    }
    static public String move(Move m) {
        if (m!=null) {
            return square(m.from())+square(m.to());
        }
        return "";
    }
    static String piece(int p) {
	String o="";
	if(p>0){o="w";}else{o="b";p=(int)(-p);}
	if(p==1)o+="p";
	if(p==2)o+="n";
	if(p==3)o+="b";
	if(p==4)o+="r";
	if(p==5)o+="q";
	if(p==6)o+="k";
	return o;
    }
    static String piece(Piece x) {
	int p = x.value();
	String o="";
	if(p>0){o="w";}else{o="b";p=(int)(-p);}
	if(p==1)o+="p";
	if(p==2)o+="n";
	if(p==3)o+="b";
	if(p==4)o+="r";
	if(p==5)o+="q";
	if(p==6)o+="k";
	return o;
    }
    static String ul_symbol(int p) {
	String o="";
	int c = p;
	if(p>0){}else{p=(int)(-p);}
	if(p==1)o+="p";
	if(p==2)o+="n";
	if(p==3)o+="b";
	if(p==4)o+="r";
	if(p==5)o+="q";
	if(p==6)o+="k";
	if(c<0)	{o=o.toLowerCase();}
	    else{o=o.toUpperCase();}
	if(p==0)o=".";
	return o;
    }
    static public String board(int[] b) {
	String out = "";
	for(int i=0;i<64;i++) {
	    if ((i%8==0)&&(i>0)) out+="\n";
	    out+=ul_symbol(b[i])+" ";
	}
	return out;
    }
    static public String game(Game g) {
        int[] b = g.dump();
        Moves m = g.getMoves();
        String out = "";
	for(int r=0;r<13;r++) {
            String l = "";
            if (r==0) {
                l+="                       ";
            }
            if (r==1) {
                l+="    -----------------  ";
            }

            // board
            if ((r>1)&&(r<10)) {
                l+=" "+rank(r-2)+" | ";
                for(int i=0;i<8;i++) {
                    l+=ul_symbol(b[i+(r-2)*8])+" ";
                }
                l+="| ";
            }
            if (r==10) {
                l+="    -----------------  ";
            }
            if (r==11) {
                l+="     ";
                for(int i=0;i<8;i++) {
                    l+=file(i)+" ";
                }
                l+="  ";
            }
            // listmove
            l+=number(r)+move(m.getMove(2*r))+" "+move(m.getMove(2*r+1));
            l+="\n";
            out+=l;
	}
	return out;
    }
    static public String board(int[] b, Moves m) {
	return "";
    }
    static public int square(char c, char r) {
	return (int)(((int)c-97)+(56-(int)r)*8);
    }
    static public int square(int c, int r) {
	return (int)((c-97)+(56-r)*8);
    }
    static public int[] move(String m) {
	int f = square(m.charAt(0),m.charAt(1));
	int t = square(m.charAt(2),m.charAt(3));
	return new int[] {f,t};
    }
    static public String variant(int[][] path, int level) {
	String p = "";
	for (int i=0;i<level;i++) {
	    p+=Less.move(path[i])+" ";
	}
	return p;
    }
    static public String fixlen(String s, int l) {
	if (s.length()>l) {
	    return s.substring(0,l-3)+"...";
	}
	while(s.length()<l)s+=" ";
	return s;
    }
    static public String symbol(int s) {
        return ul_symbol(s);
    }
    static public String number(int n) {
        String s = ""+(n+1)+". ";
        return spaces(5-s.length())+s;
    }
    static public String spaces(int n) {
        String s = "";
        for(int i=0;i<n;i++) s+=" ";
        return s;
    }
}
